﻿namespace Homework14
{
    internal class Program
    {
        /// <summary>
        /// 输出日历
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //第一行输出今天的日期
            //先清屏
            Console.Clear();
            //改变字体颜色为蓝色
            Console.ForegroundColor = ConsoleColor.Blue;
            //输出今天的日期
            Console.WriteLine(DateTime.Today.ToString("yyyy年MM月dd日"));
            //将字体颜色改为白色
            Console.ForegroundColor = ConsoleColor.White;
            //分隔线
            Console.WriteLine(new string('_', 40));
            //输出周一到周日
            Console.WriteLine("一  二  三  四  五  六  日");
            //得到此月第一天
            DateTime firstDayOfThisMonth = new DateTime(DateTime.Today.Year,
                                                        DateTime.Today.Month,
                                                        1);
            //得到第一周周一
            DateTime firstDayOfFirstWeek = firstDayOfThisMonth.AddDays(1 - (int)firstDayOfThisMonth.DayOfWeek);
            //得到上月最后一天
            DateTime lastDayOfThisMonth = firstDayOfThisMonth.AddMonths(1).AddDays(-1);
            //get weekdayoflastdayoflastday 如果是周日，就是最后一天；否则还要取所在周的周日
            //周日的weekday=0，此处要注意
            int weekdayOfLastDayOfThisMonth = (int)lastDayOfThisMonth.DayOfWeek;
            //得到上周周日
            DateTime lastDayOfLastWeek;
            if (weekdayOfLastDayOfThisMonth == 0)
            {
                //本月最后一天是周日
                lastDayOfLastWeek = lastDayOfThisMonth;
            }
            else
            {
                lastDayOfLastWeek = lastDayOfThisMonth.AddDays(7 - weekdayOfLastDayOfThisMonth);
            }
            //Console.WriteLine(lastDayOfLastWeek);
            for (DateTime thisDay = firstDayOfFirstWeek; thisDay <= lastDayOfLastWeek; thisDay = thisDay.AddDays(1))
            {
                //将当天日期，字体颜色改为黄色，背景颜色改为红色
                if (thisDay.Month == DateTime.Today.Month && thisDay.Day == DateTime.Today.Day)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.Red;
                }
                //调节排版
                if (thisDay.Day < 10)
                    Console.Write(" ");
                Console.Write(thisDay.Day.ToString(""));
                //将字体颜色调回白色，背景颜色调回黑色
                Console.ForegroundColor = ConsoleColor.White;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Write("  ");
                //一周结束后换行
                if ((int)thisDay.DayOfWeek == 0)
                    Console.WriteLine();

            }
        }
    }
}
